package com.example.mr5.mygame.game2048;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.mr5.mygame.R;
import com.example.mr5.mygame.utils.AdID;

import cn.waps.AppConnect;

/**
 * Created by Mr.5
 * Date：2016/5/8.
 * Time：14:17
 */
public class Game2048Activity extends Activity implements View.OnClickListener {

    private int score = 0;
    private TextView tvScore, tvBestScore;
    private LinearLayout root = null;
    private Button btnNewGame;
    private GameView gameView;
    private AnimLayer animLayer = null;
    private String map = "";
    public static final String SP_KEY_BEST_SCORE = "bestScore";
    public static final String SP_KEY_SCORE = "score";
    public static final String SP_KEY_MAP = "map";
    public static final boolean isFirstPlay = true;
private ImageView barBack;
private TextView barTitle;

    private static Game2048Activity game2048Activity = null;

    public Game2048Activity() {
        game2048Activity = this;
    }

    public static Game2048Activity getMainActivity() {
        return game2048Activity;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_game2048);

        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.bar);

        AppConnect.getInstance("95cec2974bd7db4551fc275772d992a9", AdID.ID, this);

        AppConnect.getInstance(this);

        initData();
        initView();
    }
    private void initView(){
        barBack = (ImageView) findViewById(R.id.header_left_btn);
        barBack.setOnClickListener(this);
        barTitle = (TextView) findViewById(R.id.header_text);
        barTitle.setText(getTitle());

        root = (LinearLayout) findViewById(R.id.container);
        root.setBackgroundColor(0xfffaf8ef);

        tvScore = (TextView) findViewById(R.id.tvScore);
        tvBestScore = (TextView) findViewById(R.id.tvBestScore);

        gameView = (GameView) findViewById(R.id.gameView);

        btnNewGame = (Button) findViewById(R.id.btnNewGame);
        btnNewGame.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gameView.startGame();
            }
        });

        animLayer = (AnimLayer) findViewById(R.id.animLayer);
    }

    private void initData(){
        score = getPreferences(MODE_PRIVATE).getInt(SP_KEY_SCORE,0);
        map = getPreferences(MODE_PRIVATE).getString(SP_KEY_MAP,"");
    }

    public void clearScore() {
        score = 0;
        showScore();
    }

    public void showScore() {
        tvScore.setText(score + "");
    }

    public void addScore(int s) {
        score += s;
        showScore();

        int maxScore = Math.max(score, getBestScore());
        saveBestScore(maxScore);
        showBestScore(maxScore);
    }

    public void saveBestScore(int s) {
        SharedPreferences.Editor e = getPreferences(MODE_PRIVATE).edit();
        e.putInt(SP_KEY_BEST_SCORE, s);
        e.commit();
    }

    public int getBestScore() {
        return getPreferences(MODE_PRIVATE).getInt(SP_KEY_BEST_SCORE, 0);
    }

    public void showBestScore(int s) {
        tvBestScore.setText(s + "");
    }

    public AnimLayer getAnimLayer() {
        return animLayer;
    }

    @Override
    protected void onPause() {
        super.onPause();
        SharedPreferences.Editor e = getPreferences(MODE_PRIVATE).edit();
        e.putInt(SP_KEY_SCORE,score);
        e.putString(SP_KEY_MAP, gameView.getMap());
        e.commit();
    }

    public String getMap() {
        return map;
    }

    public void setMap(String map) {
        this.map = map;
    }


    public void costScore(int score){
        AppConnect.getInstance(this).spendPoints(score);
    }

    public void gotoEarnScore(){
        AppConnect.getInstance(this).showAppOffers(this);
//        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.header_left_btn){
            showDialog();
        }
    }
    @Override
    public void onBackPressed() {
        showDialog();
    }

    private void showDialog() {
        new AlertDialog.Builder(Game2048Activity.this).setTitle("你好")
                .setMessage("是否退出游戏？")
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                }).setNegativeButton("否", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        }).show();
    }
}
